import { observable, action } from 'mobx'
import * as api from 'lib/constants/api'
import { post } from 'lib/net'
import Toast from 'components/toast'

// class AddressModel {
//   constructor (data) {
//     if (data) {
//     }
//   }
// }

export default class PasswordForgetVM {
  constructor (props) {
    this.props = props
    this.PageInitialization()
  }

  @observable test = ''

  // 验证码value
  @observable codeValue = '获取验证码'

  // 验证码样式class
  @observable codeClass = 'get-code'

  // 是否禁用按钮
  @observable codeDisable = false

  // 手机号
  @observable phoneNumber = ''

  // 验证码
  @observable userCode = ''

  // 新密码
  @observable password = ''

  // 倒计时60秒
  @observable setTime = 59

  // 获取页面数据
  @action onChange = (v, key) => {
    switch (key) {
      case 'PhoneNumber':
        this.phoneNumber = v
        break
      case 'UserCode':
        this.userCode = v
        break
      case 'Password':
        this.password = v
        break
    }
  }

  // 倒计时
  @action task = () => {
    if (this.setTime === 0 && this.userCode !== '') {
      this.codeClass = 'get-code'
      this.codeValue = '获取验证码'
      this.codeDisable = false
      this.setTime = 59
      return
    } else if (this.setTime === 0 && this.userCode === '') {
      this.codeClass = 'get-code'
      this.codeValue = '重新获取'
      this.codeDisable = false
      this.setTime = 59
      return
    } else {
      this.codeDisable = true
      this.codeClass = 'get-coding'
      if (this.setTime < 10) {
        this.codeValue = '重新发送(0' + this.setTime + ')'
      } else {
        this.codeValue = '重新发送(' + this.setTime + ')'
      }
      this.setTime--
    }
    setTimeout(() => { this.task() }, 1000)
  }

  // 验证手机号是否符合正则
  @action phoneCodeValid = (number) => {
    this.number = number
    this.number = Number(this.myTrim(this.number, 'g'))
    let myreg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/
    if (!myreg.test(this.number)) {
      Toast.fail('请输入正确手机号')
      return false
    } else {
      return true
    }
  }

  // 去除字符串中的空格
  @action myTrim = (str, global) => {
    let result
    result = str.toString().replace(/(^\s+)|(\s+$)/g, '')
    if (global.toLowerCase() === 'g') {
      result = result.replace(/\s/g, '')
    }
    return result
  }

  // 获取验证码事件
  @action handleGetCode = async () => {
    if (this.phoneCodeValid(this.phoneNumber) === true) {
      let params = {
        Phone: this.myTrim(this.phoneNumber, 'g'),
        Type: 4,
        Operate: 0
      }
      let response = await post(api.GETCODE, params)
      if (response.Success) {
        this.task()
      }
    }
  }

  // 提交修改
  @action handleChangePassWord = async () => {
    if (this.phoneCodeValid(this.phoneNumber) === false) {
      return false
    } else if (this.userCode === '') {
      Toast.fail('请输入验证码')
    } else if (this.password.length <= 5) {
      Toast.fail('请输入6-16位长度的密码')
    } else {
      let params = {
        PhoneNum: this.myTrim(this.phoneNumber, 'g'),
        PassWord: this.password,
        Code: this.userCode
      }
      let response = await post(api.SETPASSWORD, params)
      if (response.Success) {
        Toast.success('修改密码成功')
        window.localStorage.setItem('WZPassWord', '')
        setTimeout(() => {
          this.props.history.push('/center')
        }, 1000)
      } else {
        Toast.fail(response.Message)
      }
    }
  }

  @action PageInitialization = async () => {
    this.test = '9999999'
    // const UserId = sessionStorage.getItem('userId')
    // let response = await post(api.ADDRESSGETLIST, { UserId: UserId })
    // if (response.Success) {
    //   this.addressLength = response.Value.length
    //   this.addressList = response.Value
    // }
  }
}
